/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovxf.util;

import java.lang.reflect.Array;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import devonframework.front.channel.context.LActionContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import egovxf.service.impl.PatternServiceImpl;


public class ConventionUtil {

	static Log log = LogFactory.getLog(ConventionUtil.class);
	
    /**
     * URL 기반으로 Query ID를 추출한다. 
     *
     * @return Query ID
     */
    public static String getQueryId(String suffix) {
    	
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

    	String queryId = null;   	
    	String uri = (request == null ? "" : request.getRequestURI());	
    	String url     = uri.replaceAll( request.getContextPath(), "" );
        String urlPrev = url.substring( 1, url.toLowerCase().lastIndexOf(".") );


        if (suffix=="") {
        	queryId = urlPrev.replaceAll( "/", "." );
        } else {
        	queryId = urlPrev.replaceAll( "/", "." ).concat(suffix);
        }
        return queryId;

    }

    public static String getQueryId() {
    	return getQueryId("");
    }
    /**
     * JSP 경로에서 업무구분코드를 추출한다.
     *
     * @return 업무구분코드
     */
    public static String getPartCd() {

        String url = "";

        try{
        	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            url = getURI().replaceAll( request.getContextPath(), "" );

        } catch( NullPointerException e ) {
            return "";
        }

        int idxStart = url.indexOf( "/", 1 );

        if( idxStart < 0 ) return "";

        int idxEnd   = url.indexOf( "/", idxStart + 1 );

        if( idxEnd < 0 ) return "";

        return url.substring( idxStart + 1, idxEnd );

    }

    /**
     * 호출 URI를 가져온다.
     *
     * @return URI
     */
    public static String getURI() {
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        return request == null ? "" : request.getRequestURI();
    }


    /**
     * JSP 명을 가져온다.
     *
     * <pre>
     * 호출 URI 가 [/afs/sample/test.jsp] 라면, test.jsp 를 반환받는다.
     * </pre>
     *
     * @return JSP 명
     */
    public static String getJspName() {

          String url     = getURI();
          String urlPrev = url.substring( 0, url.toLowerCase().lastIndexOf(".jsp") );

          return urlPrev.substring( urlPrev.lastIndexOf("/") + 1 );

    }

    /**
     * 현재 화면이 로딩될 때 필요한 CSS 파일명을 구한다.
     *
     * @return CSS파일명
     */
    public static String getCurrentCss() {

        String url = getURI();
        
        if( "".equals(url) ) return "";
        
        int index = url.toLowerCase().lastIndexOf( ".jsp" );
        
        if( index <= 0 ) return "";
        
        return url.substring( 0, index ) + ".css";

    }

    /**
     * 현재 화면이 로딩될 때 필요한 JS 파일명을 구한다.
     *
     * @return JS파일명
     */
    public static String getCurrentJs() {

        String url = getURI();

        return "".equals(url) ? "" : url.substring( 0, url.toLowerCase().lastIndexOf(".jsp") ) + ".js";
    }

    /**
     * 화면호출 주소의 마지막 확장자를 구한다.
     *
     * <pre>
     * http://localhost:8101/AFS/index.jsp 로 호출될 경우
     *
     * JSPUtil.getUrlExt(); -> jsp 를 반환
     * </pre>
     *
     * @return 주소의 확장자
     */
    public static String getUrlExt() {
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        if( request == null ) return "";

        String url = request.getServletPath().toLowerCase();

        String callType = url.substring( url.lastIndexOf(".") + 1 );

        return callType;

    }

}
